package com.voole.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import com.voole.entity.SysAuthority;
import com.voole.entity.SysCatalog;
import com.voole.entity.SysAction;

@Repository
public interface SysActionDao {

	@Select(value="SELECT * FROM sys_action ORDER BY sort")
	List<SysAction> getList();
	
	@Select(value="SELECT id FROM sys_action ORDER BY sort")
	List<Integer> getColumnActionId();
	
	@Select(value="SELECT * FROM sys_action WHERE resourceid = #{param} ORDER BY sort")
	List<SysAction> getListByResourceId(@Param(value="param")Integer resourceId);
	
	@Select(value="SELECT sa.code FROM sys_action sa LEFT JOIN sys_resource sr ON sa.resourceid = sr.id WHERE sr.url LIKE '%${resourceUrl}%' AND sa.id IN (${actionIds}) AND sa.isbutton=0")
	List<String> getColumnCodeForSecurityButton(@Param(value="resourceUrl")String resourceUrl, @Param(value="actionIds")String actionIds);
	
	@Select(value="SELECT * FROM sys_action WHERE id = ${param}")
	SysAction getSysAction(@Param(value="param")Integer actionId);
	
	@Insert("INSERT INTO sys_action (`actionname`, `resourceid`, `url`, `isbutton`, `code`, `note`, `sort`, `status`) VALUES (#{actionname},#{resourceid},#{url},#{isbutton},#{code},#{note},#{sort},#{status})")  
	@Options(useGeneratedKeys = true, keyProperty = "id")
	Integer addSysAction(SysAction sysAction);
	
	@Update("UPDATE sys_action SET actionname=#{actionname}, resourceid=#{resourceid}, url=#{url}, isbutton=#{isbutton}, code=#{code}, note=#{note}, sort=#{sort}, status=#{status} WHERE id=#{id} ")  
	Integer updateSysAction(SysAction sysAction);
	
	@Delete(value="DELETE from sys_action where id = #{actionId}")
	Integer delSysAction(Integer actionId);

	@Delete(value="DELETE from sys_action where resourceid = #{resourceId}")
	Integer delListByResourceId(Integer resourceId);
	
	@Delete(value="DELETE from sys_action where id in(select sacids.id  from ( select sac.id FROM sys_action sac LEFT JOIN sys_resource sr ON sac.resourceid = sr.id LEFT JOIN sys_catalog sc ON sr.catalogid = sc.id WHERE sc.id = #{authorityId} ) sacids )")
	Integer delListByCatalogId(Integer catalogId);
	
	@Delete(value="DELETE from sys_action where id in(select sacids.id  from ( select sac.id FROM sys_action sac LEFT JOIN sys_resource sr ON sac.resourceid = sr.id LEFT JOIN sys_catalog sc ON sr.catalogid = sc.id LEFT JOIN sys_authority sau ON sc.authorityid = sau.id WHERE sau.id = #{authorityId} ) sacids )")
	Integer delListByAuthorityId(Integer authorityId);
	
}

